﻿using Autodesk.Revit.UI;
using FloatDemo.Models;
using FloatDemo.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Point = System.Windows.Point;

namespace FloatDemo.Methods
{
    public class FloatWindows
    {
        private static FloatWindows _instance;
        private FloatView _baseFloatView = null;
        /// <summary>
        /// 取得单例对象
        /// </summary>
        public static FloatWindows Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FloatWindows();
                }
                return _instance;
            }
        }
        /// <summary>
        /// 取得单例窗口
        /// </summary>
        private FloatView MainWindows
        {
            get
            {
                if (_baseFloatView == null) _baseFloatView = new FloatView();
                return _baseFloatView;
            }
        }
        /// <summary>
        /// 返回窗体的高度
        /// </summary>
        public double Height
        {
            get { return MainWindows.Height; }
        }
        /// <summary>
        /// 返回窗体的宽度
        /// </summary>
        public double Width
        {
            get { return MainWindows.Width; }
        }

        /// <summary>
        /// 鼠标移动点
        /// </summary>
        public Point CanvasMousePoint { get; set; }
        /// <summary>
        /// 鼠标移动点缓存
        /// </summary>
        public Point CacheCanvasMousePoint { get; set; }
        /// <summary>
        /// 视图移动后窗口重新定位
        /// </summary>
        /// <param name="rec"></param>
        public void ChangeLocation(Rectangle rec)
        {
            MainWindows.Left = rec.Left;
            MainWindows.Top = rec.Top;
            MainWindows.Width = rec.Right - rec.Left;
            MainWindows.Height = rec.Bottom - rec.Top;
            MainWindows.Canvas.Children.Clear();
            if (CacheCanvasMousePoint != null)
                CacheCanvasMousePoint = new Point(CanvasMousePoint.X, CanvasMousePoint.Y);
        }
        /// <summary>
        /// 打开窗体
        /// </summary>
        public void Show()
        {
            MainWindows.Show();
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public void Closed()
        {
            _baseFloatView?.Close();
            _baseFloatView = null;
        }
        /// <summary>
        /// 主窗口添加线
        /// </summary>
        /// <param name="line"></param>
        public void Add(CanvasPoint start, CanvasPoint end)
        {
            MainWindows.Canvas.Children.Add(new System.Windows.Shapes.Line()
            {
                X1 = start.Left,
                X2 = end.Left,
                Y1 = start.Top,
                Y2 = end.Top,
                Stroke = new SolidColorBrush(Colors.Black)
            });
        }
        /// <summary>
        /// 添加文字
        /// </summary>
        /// <param name="circlePoint"></param>
        /// <param name="text"></param>
        public void CreateText(CanvasPoint circlePoint, string text)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = text;
            textBlock.Foreground = new SolidColorBrush(Colors.Black);
            textBlock.FontSize = 25;
            textBlock.TextAlignment = TextAlignment.Center;
            textBlock.HorizontalAlignment = HorizontalAlignment.Center;
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            //textBlock.Height = 40;
            //textBlock.Width = 40;
            Canvas.SetLeft(textBlock, circlePoint.Left);
            Canvas.SetTop(textBlock, circlePoint.Top);
            MainWindows.Canvas.Children.Add(textBlock);
        }
        /// <summary>
        /// 判断点是否移动了
        /// </summary>
        /// <returns></returns>
        public bool PointIsMove()
        {
            return CacheCanvasMousePoint == null || !(CanvasMousePoint.X == CacheCanvasMousePoint.X && CanvasMousePoint.Y == CacheCanvasMousePoint.Y);
        }

        private FloatWindows() { }

    }
}
